<?php
declare (strict_types=1);

namespace app\controller\admin;

use app\BaseController;
use app\model\Promotion;
use app\model\User as UserM;
use app\service\SysUserService;
use think\annotation\route\Group;
use think\annotation\Route;
use \app\model\Goods as GoodsM;
use \app\model\Order as OrderM;
use think\facade\Db;
use app\middleware\CheckAdmin;
use think\annotation\route\Middleware;

/**
 * 商品
 * Class Goods
 * @package app\controller\admin
 * @Group("admin/goods")
 * @Middleware({CheckAdmin::class})
 */
class Goods extends BaseController
{

    /**
     * 登录
     * @Route("createGoods", method="POST")
     */
    public function create()
    {
        //接收数据
        $data = [
            'provider_id' => input('provider_id', '', 'trim'),
            'supplier_id' => input('supplier_id', '', 'trim'),
            'goods_third_id' => input('goods_third_id', '', 'int'),
            'goods_type' => input('goods_type', '', 'trim'),
            'third_type_id' => input('third_type_id', '', 'int'),
            'goods_pic' => input('goods_pic', '', 'trim'),
            'goods_title' => input('goods_title', '', 'trim'),
            'goods_content' => input('goods_content', '', 'trim'),
            'finish_con' => input('finish_con', '', 'trim'),
            'finish_where' => input('finish_where', '', 'trim'),
        ];

        if (empty($data['supplier_id']) || empty($data['goods_title']) || empty($data['finish_con'])  || empty($data['finish_where'])) {
            return json_error(10002);
        }

        $suppliers = config('supplier');
        if (in_array($data['supplier_id'], $suppliers)) {
            return json_error(10004);
        }

        $GoodsM = new GoodsM();
        $result = $GoodsM->save($data);
        return json_ok($result, 200);
    }

    /**
     * 登录
     * @Route("updateGoods", method="POST")
     */
    public function update()
    {
        //接收数据
        $data = [
            'provider_id' => input('provider_id', '', 'int'),
            'supplier_id' => input('supplier_id', '', 'int'),
            'goods_id' => input('goods_id', '', 'int'),
            'goods_type' => input('goods_type', '', 'trim'),
            'goods_pic' => input('goods_pic', '', 'trim'),
            'goods_title' => input('goods_title', '', 'trim'),
            'goods_content' => input('goods_content', '', 'trim'),
            'finish_con' => input('finish_con', '', 'trim'),
            'finish_where' => input('finish_where', '', 'trim'),
            'third_type_id' => input('third_type_id', '', 'int'),
            'goods_third_id' => input('goods_third_id', '', 'int'),
        ];


        if (empty($data['goods_id'])) {
            return json_error(10002);
        }

        $oneGoods = (new GoodsM())->where('goods_id', $data['goods_id'])->find();
        if (empty($oneGoods)) {
            return json_error(10004);
        }

        $data['provider_id'] && $oneGoods->provider_id = $data['provider_id'];
        $data['supplier_id'] && $oneGoods->supplier_id = $data['supplier_id'];
        $data['goods_type'] && $oneGoods->goods_type = $data['goods_type'];
        $data['goods_pic'] && $oneGoods->goods_pic = $data['goods_pic'];
        $data['goods_title'] && $oneGoods->goods_title = $data['goods_title'];
        $data['goods_content'] && $oneGoods->goods_content = $data['goods_content'];
        $data['finish_con'] && $oneGoods->finish_con = $data['finish_con'];
        $data['finish_where'] && $oneGoods->finish_where = $data['finish_where'];
        $data['goods_third_id'] && $oneGoods->goods_third_id = $data['goods_third_id'];
        $data['third_type_id'] && $oneGoods->third_type_id = $data['third_type_id'];
        $result = $oneGoods->save($data);
        return json_ok($result, 200);
    }


    /**
     * 登录
     * @Route("list", method="GET")
     */
    public function list()
    {
        $goodsId = input('goodsId', '', 'int');
        $page = input('page', 1, 'int');
        $limit = input('limit', 50, 'int');

        $uid = $this->request->sys_user->id;
        $role = $this->request->sys_user->role;

        $goodsM = new GoodsM();
        if ($goodsId) {
            $goodsM = $goodsM->where('goods_id', $goodsId);
        }

        if ($role == UserM::USER_ROLE_ADMIN) {
            $list = $goodsM->paginate(['page' => $page, 'list_rows' => $limit]);
        } else {
            $list = Db::name(GoodsM::$_table)->alias('g')
                ->field('g.*')
                ->join(Promotion::$_table . ' p', ' g.goods_id = p.goods_id')
                ->where('p.user_id',$uid)
                ->paginate(['page' => $page, 'list_rows' => $limit]);
        }

        return json_ok($list,200);
    }

}
